<template>
  <div>
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-tickets"></i>
        <span>秒杀时间段列表</span>
      </div>
    <div style="margin: 10px">
      <el-button type="primary" @click="handleAdd">添加<i class="el-icon-circle-plus-outline"></i>  </el-button>
    </div>

    <el-table :data="tableData"  border>
      <el-table-column prop="id" label="编号" width="45" align="center">
      </el-table-column>
      <el-table-column prop="name" label="秒杀时间段名称" width="120" align="center">
      </el-table-column>
      <el-table-column prop="startTime" label="每日开始时间" width="100" align="center">
      </el-table-column>
      <el-table-column prop="endTime" label="每日结束时间" width="100" align="center">
      </el-table-column>
      <el-table-column label="启用" prop="status"  width="80" align="center">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="changeSwitch(scope.row.id)"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click = "handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="handleDel(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>>
      </el-table-column>
    </el-table>

    <div style="padding: 10px 0">
    </div>
    </el-card>
    <el-dialog title="添加时间段" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="80px" size="small">
        <el-form-item label="秒杀时间段名称" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否启用">
          <template>
            <el-radio v-model="form.status" label="1">上线</el-radio>
            <el-radio v-model="form.status" label="0">下线</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="每日开始时间">
          <el-time-picker
              v-model="form.startTime"
              value-format="HH:mm:ss"
              placeholder="选择开始时间">
          </el-time-picker>
        </el-form-item>
        <el-form-item label="每日结束时间">
          <el-time-picker
              v-model="form.endTime"
              value-format="HH:mm:ss"
              placeholder="选择结束时间">
          </el-time-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Session",
  data(){
    return {
      tableData: [],
      productName:"",
      dialogFormVisible:false,
      multipleSelection:[],
      form:{}
    }
  },
  created(){
    this.load()
  },
  methods:{
    load(){
      //请求查询数据
      this.request.get("/spikeSessions",{
      }).then(res =>{
        this.tableData = res.data
      })
    },
    handleAdd(){
      this.dialogFormVisible = true
      this.form = {}
    },
    save(){
      this.request.post("/spikeSessions",this.form).then(res=>{
        if(res){
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        }else{
          this.$message.error("保存失败")
          this.dialogFormVisible = false
        }
      })
    },
    handleEdit(row){
      this.form = row
      this.dialogFormVisible = true
      this.load()
    },
    changeSwitch(id){
      this.request.put("/spikeSessions/"+id).then(res=>{
        if(res){
          this.$message.success("修改成功")
          this.load()
        }else{
          this.$message.error("修改失败")
        }
      })
    },
    handleDel(id){
      this.request.delete("/spikeSessions/"+id).then(res=>{
        if(res){
          this.$message.success("删除成功")
          this.load()
        }else{
          this.$message.error("删除失败")
        }
      })
    }
  }
}
</script>

<style scoped>

</style>